using PKGames.Audio;
using System;
using UnityEngine;
using UnityEngine.UI;

namespace VoxelEngine.UI
{
	public class BlockUiCell : MonoBehaviour
	{
		[SerializeField]
		private Transform _cellHolder;

		[SerializeField]
		private Button _button;

		[SerializeField]
		private Image _blockImage;

		[SerializeField]
		private Image _blockOutline;

		[SerializeField]
		private RectTransform _outlineRectTransform;

		private Action<BlockType> _onBlockSelected;

		private BlockType _blockType;

		private readonly Vector3 ScaleOnSelect = new Vector3(1.056f, 1.056f, 1.056f);

		public void Setup(BlockSelectionItem blockData, Action<BlockType> onBlockSelected)
		{
			_blockImage.sprite = blockData.Icon;
			_blockType = blockData.Type;
			base.name = blockData.Type.ToString();
			_onBlockSelected = (Action<BlockType>)Delegate.Combine(_onBlockSelected, onBlockSelected);
			SetupOutlineForBlockType();
			_button.onClick.AddListener(OnBlockSelected);
		}

		private void SetupOutlineForBlockType()
		{
			if (BlockTypes.IsHalfBlock(_blockType))
			{
				_outlineRectTransform.offsetMin = new Vector2(0f, 20f);
				_outlineRectTransform.offsetMax = new Vector2(0f, -20f);
			}
		}

		private void OnDestroy()
		{
			_button.onClick.RemoveAllListeners();
		}

		private void OnBlockSelected()
		{
			MonoSingleton<AudioManager>.Instance.Play("select_simple");
			_onBlockSelected.Fire(_blockType);
            AdManager.ShowNative();
        }

		public void UpdateSelection(BlockType blockTypeSelected)
		{
			bool flag = blockTypeSelected == _blockType;
			_cellHolder.localScale = ((!flag) ? Vector3.one : ScaleOnSelect);
			_blockOutline.gameObject.SetActive(flag);
		}
	}
}
